package exampleQuestions;

public class InsrtionSort {

	static int[] array = new int[] { 5, 9, 4, 0, 14, 6, 14, 99, 3, 44, 17 };

	public static void main(String[] args) {

		sort(array);

		printArray(array);

	}

	private static void sort(int[] array2) {

		for (int i = 1; i < array2.length; i++) {

			int l = 0;
			int p = i - 1;
			int v = array[i];
			
			while (l <= p) {
				int mid = (l + p) >> 1;
				if (v < array2[mid]) {
					p = mid - 1;
				} else {
					l = mid + 1;
				}
			}

			for (int j=i-1; j >= l; j--) {
				swap(array2, j+1, j);
			}
		}

	}

	private static void swap(int[] array, int i, int j) {
		int tmp = array[i];
		array[i] = array[j];
		array[j] = tmp;

	}

	private static void printArray(int[] array2) {
		for (int a : array) {
			System.out.print(a + " ");
		}
	}

}
